// pages/strategy/details/details.js
import {
  getAttack,
  lookAttack,
  lookMember,
  judgeMember,
  maybeLike,
  discussList
} from '../../../utils/api.js'
import {login} from "../../../utils/api";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    shareToken: "",
    count: "",
    currentSwiper: 0,
    autoplay: true,
    indicatorDots: false,
    interval: 3000,
    duration: 1000,
    titlename: "",
    create_time: "",
    attack_content: [],
    price: "",
    oldPrice: "",
    notice_content: [],
    click_num: "",
    hideFlag: true, //true-隐藏  false-显示
    animationData: {},
    showActivities: {
      titlename: "",
      create_time: "",
      attack_content: [],
      price: "",
      oldPrice: "",
      notice_content: [],
      click_num: "",
      imgs: [],
      is_sell_safe: "",
      shppImg: "",
      shppTitle: "",
      maybeListData: []
    },
    loginData: {
      code: "",
      nickname: "",
      head_img: "",
      sex: ""
    },
    attack: {
      p: "",
      searchKey: "",
      shop_type_one: "",
      shop_type_two: ""
    },
    gonglv: {
      id: "",
      attack_id: "",
    },
    shoppCarParameter: {
      active_id: '',
      attack_id: '',
    },
    is_sell_safe: false,
    shopCarData: {},
    shppImg: "",
    shppTitle: "",
    judge: {},
    member: "",
    activityID: "",
    maybeData: {},
    maybeListData: [],
    discussData: {
      active_id: "",
      type: "",
      p: 1,
    },
    discussList: [],
    comment_id: '',
    sision: true,
    options: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      options: options
    });
    wx.getStorage({
      key: 'sission',
      success: function (res) {
        //获取用户头像
        wx.getUserInfo({
          success: function (res) {
            let userInfo = res.userInfo;
            that.setData({
              headImg: userInfo.avatarUrl
            })
          }
        });
        that.setData({
          sision: !!res.data
        });
        that.init(options);
      },
      fail: function (res) {
        that.setData({
          sision: false,
        })
      }
    });
  },
  bindGetUserInfo(res) {
    if (res.detail.userInfo) {
      this.setData({
        sision: true,
      });
      this.init(this.data.options);
    } else {
    }
  },
  init(options) {
    var that = this;
    wx.getStorage({
      key: 'sission',
      success: function (res) {
        wx.showLoading({ title: '加载中' });
        var data = JSON.parse(options.data);
        //评论数  当active_id=-1时  需要传入攻略ID
        let type = 1;
        if (data.id == -1) {
          that.data.comment_id = data.attack_id
          type = 1;
          let attack_id_ = "shoppCarParameter.attack_id"
          that.setData({
            [attack_id_]: data.attack_id
          });
        } else {
          that.data.comment_id = data.id
          type = 2
        }
        let attack_id = 'gonglv.attack_id';
        let id = 'gonglv.id';
        let wx_token = 'gonglv.wx_token';
        let id2 = 'discussData.active_id';
        let type1 = 'discussData.type';
        let active_id = "shoppCarParameter.active_id"
        that.setData({
          [id]: data.id,
          [wx_token]: data.wx_token,
          [attack_id]: data.attack_id,
          [active_id]: data.id,
          activityID: data.id,
          [id2]: that.data.comment_id,
          [type1]: type
        });
        that.lookAttacktt()
        that.busyReplay()
        //可能喜欢
        that.maybeLove();
      },
      fail: function (res) {
        wx.login({
          success: function (res) {
            let code_temp = res.code;
            let code = "loginData.code";
            wx.getUserInfo({
              success: function (res) {
                let nickname = "loginData.nickname";
                let head_img = "loginData.head_img";
                let sex = "loginData.sex";
                that.setData({
                  [code]: code_temp,
                  [nickname]: res.userInfo.nickName,
                  [sex]: res.userInfo.gender,
                  [head_img]: res.userInfo.avatarUrl,
                });
                that.Login(options);
              }
            })
          },
          fail: function (res) {
          },
          complete: function (res) {
          },
        })
      }
    })
  },
  Login(options) {
    login(this.data.loginData).then(res => {
      this.init(options);
      wx.setStorage({
        key: "is_member",
        data: res.datas.is_member,
      })
      wx.setStorage({
        key: 'is_shop',
        data: res.datas.is_shop,
      });
      if ([2, 3].includes(res.datas.is_shop)) {
        wx.setStorage({
          key: 'is_check',
          data: true,
        });
      }
      wx.setStorage({
        key: "memberPrice",
        data: res.datas.memberPrice,
      })
      wx.setStorage({
        key: "token",
        data: res.datas.token,
      })
      wx.setStorage({
        key: 'sission',
        data: true,
      })
    })
  },

  //商家回复列表
  busyReplay() {
    discussList(this.data.discussData).then(res => {
      if (res.code === 200) {
        this.setData({
          discussList: res.datas.list,
          count: res.datas.count
        })
      }
    })
  },
  //可能喜欢
  maybeLove() {
    maybeLike(this.data.maybeData).then(res => {
      // let showActivitiesMAY = "showActivities.maybeListData"
      this.setData({
        maybeListData: res.datas
        //  [showActivitiesMAY]: res.datas
      })
    })
  },
  shoppCar(e) {
    let is_sell_safe = this.data.is_sell_safe;
    let shppImg = this.data.shppImg;
    let shppTitle = this.data.shppTitle;
    judgeMember(this.data.judge).then(res => {
      this.setData({
        member: res.datas.is_member
      })
    })
    lookMember(this.data.shoppCarParameter).then(res => {
      let that = this;
      let is_member = this.data.member
      res.datas.is_sell_safe = that.data.is_sell_safe
      res.datas.shppTitle = shppTitle
      res.datas.shppImg = shppImg
      res.datas.is_member = is_member
      res.datas.activityID = that.data.activityID
      res.datas.attack_id = that.data.shoppCarParameter.attack_id
      let data = JSON.stringify(res.datas)
      wx.navigateTo({
        url: './pay/pay?data=' + encodeURIComponent(data),
      })
    })
  },
  skipIndex(e) {
    wx.switchTab({
      url: '../../index/index',
    })
    // wx.navigateBack({
    //   url: ''
    // })
  },
  //取消
  mCancel: function (e) {
    this.hideModal();
  },
  //显示遮罩
  showModal: function () {
    var that = this;
    that.setData({
      hideFlag: false
    })
    // 创建动画实例
    var animation = wx.createAnimation({
      duration: 400, //动画的持续时间
      timingFunction: 'ease', //动画的效果 默认值是linear->匀速，ease->动画以低速开始，然后加快，在结束前变慢
    })
    this.animation = animation; //将animation变量赋值给当前动画
    var time1 = setTimeout(function () {
      that.slideIn(); //调用动画--滑入
      clearTimeout(time1);
      time1 = null;
    }, 100)
  },
  // 隐藏遮罩层
  hideModal: function () {
    var that = this;
    var animation = wx.createAnimation({
      duration: 400, //动画的持续时间 默认400ms
      timingFunction: 'ease', //动画的效果 默认值是linear
    })
    this.animation = animation
    that.slideDown(); //调用动画--滑出
    var time1 = setTimeout(function () {
      that.setData({
        hideFlag: true
      })
      clearTimeout(time1);
      time1 = null;
    }, 220) //先执行下滑动画，再隐藏模块

  },
  lookAttacktt() {
    lookAttack(this.data.gonglv).then(res => {
      wx.hideLoading();
      if (res.code === 200) {
        this.setData({
          titlename: res.datas.attack_name,
          create_time: res.datas.create_time,
          attack_content: res.datas.attack_content,
          price: res.datas.price,
          oldPrice: res.datas.consult_price,
          notice_content: res.datas.notice_content,
          click_num: res.datas.click_num,
          sell_num: res.datas.sell_num,
          imgs: res.datas.imgs,
          is_sell_safe: res.datas.is_sell_safe,
          shppImg: res.datas.imgs[0],
          shppTitle: res.datas.attack_name
        })

      } else {
        wx.showToast({
          title: res.msg,
          duration: 3000,
          icon: 'none'
        })
      }
    })
  },
  closeModal() {
    this.hideModal()
  },
  //动画 -- 滑入
  slideIn: function () {
    this.animation.translateY(0).step() // 在y轴偏移，然后用step()完成一个动画
    this.setData({
      //动画实例的export方法导出动画数据传递给组件的animation属性
      animationData: this.animation.export()
    })
  },
  //动画 -- 滑出
  slideDown: function () {
    this.animation.translateY(300).step()
    this.setData({
      animationData: this.animation.export(),
    })
  },
  swiperChange(e) {
    this.setData({
      currentSwiper: e.detail.current
    })

  },
  // 预览图片
  previewIamge1(event) {
    const nowIndex = event.currentTarget.dataset.id;
    const imgList = event.currentTarget.dataset.list
    wx.previewImage({
      current: imgList[nowIndex], //当前预览的图片
      urls: imgList, //所有要预览的图片
    })
  },
  // 预览图片
  previewIamge2(event) {
    const nowIndex = event.currentTarget.dataset.id;
    const imgList = event.currentTarget.dataset.list
    wx.previewImage({
      current: imgList[nowIndex], //当前预览的图片
      urls: imgList, //所有要预览的图片
    })
  },
  share(e) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  tiaozhaun(e) {
    var info = e.currentTarget.dataset.id;
    var id = 'gonglv.id';
    var attack_id = 'gonglv.attack_id'
    var attack_id2 = 'discussData.active_id'
    this.setData({
      [id]: info.id,
      [attack_id]: info.attack_id,
      [attack_id2]: info.id
    })

    let gonglv = JSON.stringify(this.data.gonglv)
    wx.redirectTo({
      url: './details?data=' + gonglv,
    })

    // this.lookAttacktt()
    // this.busyReplay()
    // //可能喜欢
    // this.maybeLove()
  },
  my() {
    wx.switchTab({
      url: '../../personal/personal',
    })
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    wx.showToast({
      title: '正在刷新数据...',
      icon: 'loading',
      duration: 2000
    })
    that.lookAttacktt()
    that.busyReplay()
    that.maybeLove()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   *
   */
  onShareAppMessage: function (options) {
    this.data.gonglv.wx_token = wx.getStorageSync('token');
    let gonglv = JSON.stringify(this.data.gonglv);
    var shareObj = {
      title: this.data.titlename, // 默认是当前页面，必须是以‘/’开头的完整路径
      path: '/pages/strategy/details/details', //转发时显示的图片路径，支持网络和本地，不传则使用当前页默认截图。
      imageUrl: '',
      success: function (res) {
        if (res.errMsg == 'shareAppMessage:ok') {
        }
      },
      fail: function (res) {
      },
      complete: function (res) {
      }
    }
    if (options.from == 'button') {
      var dataid = options.target.dataset; //上方data-id=shareBtn设置的值
      // 此处可以修改 shareObj 中的内容
      shareObj.path = '/pages/strategy/details/details?data=' + gonglv;
    }
    console.log(shareObj);
    return shareObj
  },
  aa(e) {
  }
})